$(document).ready(function () {
  var table = $('table#table').DataTable({
    ajax: {
      url: baseUrl + '/datatable',
      data : function(d) {
              d.headerId = headerId;
  		      return d;
        }
    },
    searchCols: [{
        search: headerId
      }],
    order: [
      [1, 'asc']
    ],
    columns: [{
    	data: 'headerId',
    	data: 'headerId',
    	defaultContent: '',
		visible: false
    },{
      data: 'id',
      sortable: false,
      defaultContent: '',
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return '<div class="checkbox"><input type="checkbox" name="ids" value="' + data + '"/><label for="ids"/></div>'
      }
 
    },{
      name: 'lineNumber',
      data: 'lineNumber'
    },{
      name: 'kitDocCode',
      data: 'kitDocCode'
    },{
      name: 'dEdi07',
      data: 'dEdi07'
    },{
      name: 'dEdi12',
      data: 'dEdi12'
    },{
      name: 'dEdi13',
      data: 'dEdi13'
    },{
      name: 'dEdi14',
      data: 'dEdi14'
    },{
      name: 'checkContainNumber',
      data: 'checkContainNumber'
    },{
      name: 'lineStatus',
      data: 'lineStatus',
      render: function (data, type, row, meta) {
        	return VALUES_MAP_DICT_TYPE && VALUES_MAP_DICT_TYPE[data] ? VALUES_MAP_DICT_TYPE[data].text : '';
        }
    }]
  });

  table.on('click', '.del', function () {
    var id = table.row($(this).parents('tr')).data().id;
    $("#delForm #ids").val(id);
    if (id) {
      bootbox.confirm("确定要删除数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    }
  });
  $(".btn.delAll").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#delForm #ids").val(ids.join(","));
    if (ids.length > 0) {
      bootbox.confirm("确定要删除选中的数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    } else {
      bootbox.alert("请选择要删除的数据.");
    }
  });

  table.on('click', '.enable', function () {
    var that = this;
    var data = table.row($(this).parents('tr')).data();
    var id = data.id;
    var enabled = data.enabled;
    bootbox.confirm("确定要更新选中的数据吗?", function(resul) {
	if (resul) {
	    $.get(baseUrl + "/enable", {
	      enabled: !enabled,
	      ids: id
	    }, function (res) {
	      if (res) {
	        data.enabled = !enabled;
	        $(that).find("i").removeClass("fa-" + (enabled ? "check-" : "") + "square-o").addClass("fa-" + (!enabled ? "check-" : "") + "square-o");
	        toastr.info('操作成功!');
	      }
	
	    });
	}});   
  });

  $('.search-form').on("submit", function (e) {
    table.draw();
    //阻止表单submit
    return false;
  });

});
